﻿//写Cookies
function setCookie(Name, Value, Days) {
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = Name + "=" + escape(Value) + ";expires=" + exp.toGMTString();
}

function getCookie(Name) {
    var Arr = document.cookie.match(new RegExp("(^|)" + Name + "=([^;]*)(:|$)"));
    if (Arr != nul) {
        return unescape(Arr[2]);
    } else {
        return null;
    }
}

function delCookie(Name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cVal = getCookie(Name);
    if (cVal != null) {
        document.cookie = Name + "=" + cVal + ";expires=" + exp.toGMTString();
    }
}

function MenuBtnClick(action, control, frameName) {
    $("#" + frameName, parent.document.body).attr("src", "/" + control + "/" + action);
}

function ReportBtnClick(action, control, winName){
    var url = "/" + control + "/" + action;
    //window.open(url, winName, "height=768, width=1024, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no", "_blank");
    window.open(url);
}

function getBytesLength(str) {
    return str.replace(/[^\x00-\xff]/g, '**').length;
};

//修改在新增，修改时按回车出错的BUG
$(function () {
    document.onkeydown = function (e) {
        var ev = document.all ? window.event : e;
        if ((ev.keyCode == 13) && (ev.srcElement.type != 'textarea')) {
            if (!(typeof (CloseWin))) {
                CloseWin(1); //处理保存事件

                ev.keyCode = 0;  //取消键值传递
                ev.returnValue = false;
            }
        } else if (ev.keyCode == 27) {
            CloseWin(0); //处理取消事件
        }
    }
    //所有的Combobox只能选择不能编辑
    //$(".easyui-combobox").attr("editable", "false");
});

$.extend($.fn.validatebox.defaults.rules, {
    minLength: {
        // 判断最小长度
        validator: function (value, param) {
            return getBytesLength($.trim(value)) >= param[0];
        },
        message: '最少输入 {0} 个字符。'
    },
    maxLength: {
        // 判断最大长度
        validator: function (value, param) {
            return getBytesLength($.trim(value)) <= param[0];
        },
        message: '最多输入 {0} 个字符。'
    },
    length: {
        validator: function (value, param) {
            var len = getBytesLength($.trim(value));
            return len >= param[0] && len <= param[1];
        },
        message: "输入内容长度必须介于{0}和{1}之间."
    },
    range: {
        validator: function (value, param) {
            var v = new Number($.trim(value));
            if (v == 'NaN') return false;
            return v >= param[0] && v <= param[1];
        },
        message: "输入数值必须介于{0}和{1}之间."
    },
    isdate: {
        validator: function (value, param) {
            return /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/i.test(value);
        },
        message: "输入内容必须是日期格式，如“2012-12-22”。"
    },
    isdatetime: {
        validator: function (value, param) {
            return /^(\d{4})\-(\d{1,2})\-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/i.test(value);
        },
        message: "输入内容必须是日期加时间格式，如“2012-12-22 08:30:30”。"
    },
    phone: {
        // 验证电话号码
        validator: function (value) {
            return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
        },
        message: '格式不正确,请使用下面格式:020-88888888'
    },
    mobile: {
        // 验证手机号码
        validator: function (value) {
            return /^(13|15|18)\d{9}$/i.test(value);
        },
        message: '手机号码格式不正确'
    },
    idcard: {
        // 验证身份证
        validator: function (value) {
            if (value.length < 15 || value.length == 16 || value.length == 17 || value.length > 18)
            { return false; }

            var Ai;
            if (value.length == 18)
            { Ai = value.substring(0, 17); }
            else
            { Ai = value.substring(0, 6) + "19" + value.substring(6, 9); }
            var iResult = /^\d+?$/i.test(Ai);
            if (!iResult)
            { return false; }

            var strYear, strMonth, strDay, strBirthDay;
            strYear = Ai.substring(6, 10);
            strMonth = Ai.substring(10, 12);
            strDay = Ai.substring(12, 14);
            strBirthDay = strYear + "-" + strMonth + "-" + strDay;
            iResult = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/i.test(strBirthDay);
            if (!iResult)
            { return false; }

            var arrVerifyCode = new Array("1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2");
            var Wi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);

            var i, TotalmulAiWi = 0;
            for (i = 0; i <= 16; i++)
            { TotalmulAiWi = TotalmulAiWi + parseInt(Ai.charAt(i)) * Wi[i]; }

            var modValue = TotalmulAiWi % 11;
            var strVerifyCode = arrVerifyCode[modValue];

            Ai = Ai + strVerifyCode;

            if ((value.length == 18) && (value != Ai))
            { return false; }

            return true;
        },
        message: '身份证号码格式不正确'
    },
    intOrFloat: {
        // 验证整数或小数
        validator: function (value) {
            return /^\d+(\.\d+)?$/i.test(value);
        },
        message: '请输入数字或者小数，并确保格式正确'
    },
    currency: {
        // 验证货币
        validator: function (value) {
            return /^\d+(\.\d+)?$/i.test(value);
        },
        message: '货币格式不正确'
    },
    qq: {
        // 验证QQ,从10000开始
        validator: function (value) {
            return /^[1-9]\d{4,9}$/i.test(value);
        },
        message: 'QQ号码格式不正确'
    },
    integer: {
        // 验证整数
        validator: function (value) {
            return /^[+]?[1-9]+\d*$/i.test(value);
        },
        message: '请输入整数'
    },
    chinese: {
        // 验证中文
        validator: function (value) {
            return /^[\u0391-\uFFE5]+$/i.test(value);
        },
        message: '请输入中文'
    },
    english: {
        // 验证英语
        validator: function (value) {
            return /^[A-Za-z]+$/i.test(value);
        },
        message: '请输入英文'
    },
    unnormal: {
        // 验证是否包含空格和非法字符
        validator: function (value) {
            return /[@#\$%\^&\*]+/g.test(value);
        },
        message: '输入值不能为空和包含其他非法字符'
    },
    username: {
        // 验证用户名
        validator: function (value) {
            return /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value);
        },
        message: '用户名不合法（字母开头，允许6-16字节，允许字母数字下划线）'
    },
    faxno: {
        // 验证传真
        validator: function (value) {
            //            return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/i.test(value);
            return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
        },
        message: '传真号码不正确'
    },
    zip: {
        // 验证邮政编码
        validator: function (value) {
            return /^[1-9]\d{5}$/i.test(value);
        },
        message: '邮政编码格式不正确'
    },
    ip: {
        // 验证IP地址
        validator: function (value) {
            var exp = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
            var reg = value.match(exp);           
            return (reg != null);
        },
        message: 'IP地址格式不正确'
    },
    name: {
        // 验证姓名，可以是中文或英文
        validator: function (value) {
            return /^[\u0391-\uFFE5]+$/i.test(value) | /^\w+[\w\s]+\w+$/i.test(value);
        },
        message: '请输入姓名'
    },
    carNo: {
        validator: function (value) {
            return /^[\u4E00-\u9FA5][\da-zA-Z]{6}$/.test(value);
        },
        message: '车牌号码无效（例：粤J12350）'
    },
    carenergin: {
        validator: function (value) {
            return /^[a-zA-Z0-9]{16}$/.test(value);
        },
        message: '发动机型号无效(例：FG6H012345654584)'
    },
    email: {
        validator: function (value) {
            return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);
        },
        message: '请输入有效的电子邮件账号(例：abc@126.com)'
    },
    msn: {
        validator: function (value) {
            return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);
        },
        message: '请输入有效的msn账号(例：abc@hotnail(msn/live).com)'
    },
    equalTo: {
        validator: function (value, param) {
            if ($("#" + param[0]).val() != "" && value != "") {
                return $("#" + param[0]).val() == value;
            } else {
                return true;
            }
        },
        message: '两次输入的密码不一致！'
    },
    dateGreatTo: {
        validator: function (value, param) {
            if ($(param).val() != "" && value != "") {
                var d1 = Date.parse(value.replace(/-/g, "/"));
                var d2 = Date.parse($(param).val().replace(/-/g, "/"));
                return d1 >= d2;
            } else {
                return true;
            }
        },
        message: '结束日期必须大于等于开始日期！'
    },
    vlGreatTo: {
        validator: function (value, param) {
            if ($(param).val() != "" && value != "") {
                var sRelNm = /(([^\[]*)?(\[([^\[]*)\])?)(.*)?/.exec(param);
                var d1 = parseInt(value);
                var d2 = parseInt($(sRelNm[2]).val());
                return d1 >= d2;
            } else {
                return true;
            }
        },
        message: '{0}！'
    },
    vlcbGreatTo: {
        validator: function (value, param) {
            if (value != "") {
                var sRelNm = /(([^\[]*)?(\[([^\[]*)\])?)(.*)?/.exec(param);
                var sClsIn = sRelNm[2].split(";");
                if (sClsIn.length = 2) {
                    try {
                        var d1 = parseInt($(sClsIn[0]).combobox("getValue"));
                        var d2 = parseInt($(sClsIn[1]).combobox("getValue"));
                        return d1 >= d2;
                    } catch (err) { }
                }
            }

            return true;
        },
        message: '{0}！'
    },
    Unique: {
        validator: function (value, param) {
            var sMatchIn = /(([^\[]*)?(\[([^\[]*)\])?)(.*)?/.exec(param);
            return $(sMatchIn[2]).text() == "";
        },  //end RelateEx
        message: "请确定输入不重复的{0}！"
    },
    ReqRelExVal: {
        validator: function (value, param) {
            //validType = "ReqRelExVal#ToClassID:V;#ToYear:V;#ToGradeLevel:C;#ToTeamID:C"
            var sMatchIn = /(([^\[]*)?(\[([^\[]*)\])?)(.*)?/.exec(param);
            var sClsIn = sMatchIn[2].split(";");
            if ((value != "") && (sClsIn.length >= 1)) {
                var isVal = true;
                for (var ii = 1; ii < sClsIn.length; ii++) {
                    var _lst = sClsIn[ii].split(":");
                    if (_lst[1] == "V")
                    { isVal = isVal && ($(_lst[0]).val() != ""); }
                    else
                    { isVal = isVal && ($(_lst[0]).combobox("getValue") != ""); }
                }

                var _ndv = sClsIn[0].split(":");
                if (_ndv[1] == "V")
                { return !isVal || ($(_ndv[0]).val() != ""); } else
                { return !isVal || ($(_ndv[0]).combobox("getValue") != ""); }
            }

            return true;
        },  //end RelateEx
        message: "需要输入正确的{0}信息！"
    },
    LmtValwExvIn: {
        validator: function (value, param) {
            var sMatchIN = /(([^\[]*)?(\[([^\[]*)\])?)(.*)?/.exec(param);
            if ((value != "") && (sMatchIN[5] != "")) {
                var sId = sMatchIN[2].split(":");
                var svlin;
                if (sId.length >= 2) {
                    if (sId[1] == "V")
                    { svlin = $(sId[0]).val(); }
                    else { svlin = $(sId[0]).combobox("getValue"); }
                } else {
                    svlin = $(sId[0]).val();
                }

                if (svlin == "") return true;
                var sVals = sMatchIN[5].split(";");
                for (var ii = 0; ii < sVals.length; ii++) {
                    var spky = sVals[ii].split(":");
                    if (spky[0].indexOf(svlin) >= 0) {
                        return (spky[1] == "") || (spky[1].indexOf(value) >= 0);
                    }
                }

                return false;  //没有找着匹配的值 
            }

            return true;
        },
        message: "请根据当前的{0}选择值！"
    },
    ReqRelValIn: {
        validator: function (value, param) {
            //validType = "ReqRelExVal#ToClassID:V[提示信息的替换内容(逗号分隔)]ValueArraySplitWith':'"
            var sMatchIn = /(([^\[]*)?(\[([^\[]*)\])?)(.*)?/.exec(param);
            if ((value != "") && (sMatchIn[2] != "") && (sMatchIn[5] != "")) {
                var vin;
                var _lst = sMatchIn[2].split(":");
                if (_lst.length >= 2) {
                    if (_lst[1] == "V")
                    { vin = $(_lst[0]).val(); }
                    else
                    { vin = $(_lst[0]).combobox("getValue"); }
                } else
                { vin = $(sMatchIn[2]).val(); }

                return (vin == "") || (sMatchIn[5].indexOf(vin) >= 0)
            }

            return true;
        },  //end RelateEx
        message: "{0}！"
    },
    NdInList: {
        validator: function (value, param) {
            //validType = "ReqRelExVal#ListClassID"
            var sMatchIn = /(([^\[]*)?(\[([^\[]*)\])?)(.*)?/.exec(param);
            if ((sMatchIn[2] != "") && (value != "")) {
                var _lst = sMatchIn[2].split(":");
                var sNmEx = "";
                var sType = "V";

                if (_lst.length >= 2) {
                    sNmEx = _lst[0];
                    sType = _lst[1];
                } else {
                    sNmEx = sMatchIn[2];
                }
                var obj = $(sNmEx);

                var sfldn = "dtLst[ii]." + obj.combobox("options").textField;
                var dtLst = obj.combobox("getData");
                if (dtLst.length >= 1) {
                    for (var ii = 0; ii < dtLst.length; ii++) {
                        try {
                            if (eval(sfldn) == value) {
                                return true;
                            }
                        } catch (err) { }
                    }
                }
                return false;
            }

            return true;
        },
        message: "请确定选择有效的值！"
    },
    NDVLNE: { //当有值时，要求相关控件有值
        validator: function (value, param) {
            var sMatchIn = /(([^\[]*)?(\[([^\[]*)\])?)(.*)?/.exec(param);
            if (sMatchIn[2] != "") {
                if (value != "") {
                    var _lst = sMatchIn[2].split(":");
                    var sNmEx = "";
                    var sType = "E";

                    if (_lst.length >= 2) {
                        sNmEx = _lst[0];
                        sType = _lst[1];
                    } else {
                        sNmEx = sMatchIn[2];
                    }
                    var obj = $(sNmEx);

                    if (sMatchIn[5] != "") { //有父类对象要求的值
                        obj = $(sNmEx, $(sMatchIn[5]));
                    }

                    if (sType == "V") {
                        return !!obj.combobox("getValue");
                    } else {
                        return !!obj.val();
                    }
                } else {
                    return false;
                }
            }
            return true;
        },
        message: "{0}!"
    },
    NDVLINLST: { //当有值时，要求相关控件有值
        validator: function (value, param) {
            var sMatchIn = /(([^\[]*)?(\[([^\[]*)\])?)(.*)?/.exec(param);
            if (sMatchIn[2] != "") {
                if (value != "") {
                    var sMatchIn = /(([^\[]*)?(\[([^\[]*)\])?)(.*)?/.exec(param);
                    if ((sMatchIn[2] != "") && (value != "")) {
                        var _lst = sMatchIn[2].split(":");
                        var sNmEx = "";
                        var sType = "V";

                        if (_lst.length >= 2) {
                            sNmEx = _lst[0];
                            sType = _lst[1];
                        } else {
                            sNmEx = sMatchIn[2];
                        }
                        var obj = $(sNmEx);

                        var sfldn = "dtLst[ii]." + obj.combobox("options").textField;
                        var dtLst = obj.combobox("getData");
                        if (dtLst.length >= 1) {
                            for (var ii = 0; ii < dtLst.length; ii++) {
                                try {
                                    if (eval(sfldn) == value) {
                                        return true;
                                    }
                                } catch (err) { }
                            }
                        }
                        return false;
                    }
                }
            }
            return true;
        },
        message: "{0}!"
    }

});
